<?php

namespace App\Http\Controllers\Front;

use App\Http\Controllers\Controller;
use App\Model\CardVideoIntroductions;
use App\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class CardVideoIntroductionsController extends Controller
{
    /**
     * @var User
     */
    private $user = null;

    public function __construct()
    {
        $this->user = Auth::user();
    }
    public function index(Request $request, CardVideoIntroductions $videoIntroductions)
    {

        $cardVideos = $videoIntroductions->where('user_id', '=', $this->user->id)->orderByDesc('created_at')->get();
        $data = [];
        foreach ($cardVideos as $cardVideo){
            $data[] = [
                'id' => $cardVideo->id,
                'title' => $cardVideo->title,
                'created_at' => $cardVideo->created_at->format('Y-m-d')
            ];
        }
        return $this->renderJson($data);
    }

    /**
     * 获取单个视频
     * @param $id
     * @param $videoIntroductions
     * @return \Illuminate\Http\Response
     */
    public function show($id, CardVideoIntroductions $videoIntroductions)
    {
        $cardVideo = $videoIntroductions->findOrFail($id);
        $data = [
            'id' => $cardVideo->id,
            'title' => $cardVideo->title,
            'content' => $cardVideo->content,
            'url' => $cardVideo->video_url,
            'created_at' => $cardVideo->created_at->format('Y-m-d')
        ];
        return $this->renderJson($data);
    }

    public function store(Request $request, CardVideoIntroductions $videoIntroductions)
    {
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
            'url' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'title' => '标题',
            'content' => '内容',
            'url' => '视频链接'
        ]);


        if ($videoIntroductions->add($request)){
            return $this->success();
        }
        return $this->error('添加失败');
    }

    public function update(Request $request, $id, CardVideoIntroductions $videoIntroductions)
    {
        $cardVideo = $videoIntroductions->findOrFail($id);
        $this->validate($request, [
            'title' => 'required',
            'content' => 'required',
            'url' => 'required'
        ], [
            'required' => ':attribute不能为空'
        ], [
            'title' => '标题',
            'content' => '内容',
            'url' => '视频链接'
        ]);

        $cardVideo->title = request('title');
        $cardVideo->content = request('content');
        $cardVideo->video_url = request('url');

        if ($cardVideo->save()){
            return $this->success();
        }
        return $this->error('修改失败');
    }

    public function destroy($id, CardVideoIntroductions $videoIntroductions)
    {
        $cardVideo = $videoIntroductions->findOrFail($id);
        try {
            $res = $cardVideo->delete();
            if ($res){
                return $this->success();
            }
        } catch (\Exception $e) {
        }
        return $this->error('删除失败');
    }
}
